package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.PollutionControlStatus;
import com.jiantong.work.impl.dao.PollutionControlDao;
import com.jiantong.work.model.PollutionControl;
import com.jiantong.work.service.PollutionControlReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Slf4j
@Service
@RpcProvider
public class PollutionControlReadServiceImpl implements PollutionControlReadService {

    private final PollutionControlDao pollutionControlDao;
    private final BreedFactoryDao breedFactoryDao;
    @Autowired
    public PollutionControlReadServiceImpl(PollutionControlDao pollutionControlDao, BreedFactoryDao breedFactoryDao) {
        this.pollutionControlDao = pollutionControlDao;
        this.breedFactoryDao = breedFactoryDao;
    }


    /**
     * 根据id查询治污中心的某一条数据
     */
    @Override
    public Response<PollutionControl> findById(Long id) {
        try {
            PollutionControl pollutionControl = pollutionControlDao.findById(id);
            if (pollutionControl == null) {
                log.error("pollutionControl(id={}) is not found", id);
                return Response.fail("pollutionControl.not.found");
            }
            return Response.ok(pollutionControl);
        } catch (Exception e) {
            log.error("failed to find pollutionControl(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("pollutionControl.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = pollutionControlDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("pollution control status (id={}) is not found", id);
                return Response.fail("pollution control.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("pollution control status (id={}) is not found", id);
            return Response.fail("pollution control status not found");
        }
    }


    /**
     * 分页查询
     * 养殖场根据status查询
     */
    @Override
    public Response<Paging<PollutionControl>> pagingPollutionControl(Integer pageNo, Integer pageSize,Integer status , Long breedFactoryId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryId",breedFactoryId);

            if (status != null){
                criteria.put("status",status);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(pollutionControlDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find pollutionControl{}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("pollutionControl.find.fail");
        }
    }

    /**
     * 养殖场合计查询
     */
    @Override
    public Response<Map> sumDryAndLiquidByBreedFactoryId(Long breedFactoryId) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryId", breedFactoryId);

            criteria.put("status",PollutionControlStatus.FINISHED.value());

            criteria.put("deleteFlag",DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(pollutionControlDao.aquacultureSum(criteria));
        }catch (Exception e){
            log.error("failed to find aquacultureSum{}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("aquacultureSum.find.fail");
        }
    }

    @Override
    public Response<Map> sumDryAndLiquidByBreedFactoryId(Map map) {

        return Response.ok(pollutionControlDao.aquacultureSum(map));

    }


    /**
     * 政府端合计查询
     */
    @Override
    public Response<Map> sumGovernment(Long breedFactoryId,Date fromDate,Date toDate) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            List<Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {

                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds",breedFactoryIds);
            else return Response.ok(Maps.newHashMap());

            if(fromDate != null) criteria.put("fromDate",fromDate);

            if(toDate != null) criteria.put("toDate",toDate);

            criteria.put("status",PollutionControlStatus.FINISHED.value());

            criteria.put("deleteFlag",DocumentsDeleteFlag.NORMAL.value());

            return Response.ok(pollutionControlDao.governmentSum(criteria));
        }catch (Exception e){
            log.error("failed to find aquacultureSum{}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("aquacultureSum.find.fail");
        }
    }


    /**
     * 政府端时间查询
     */
    @Override
    public Response<Paging<PollutionControl>> pagingPollutionControls(Integer pageNo, Integer pageSize,Long breedFactoryId,Integer breedType,Date fromDate, Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            List<Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }

            //在这里根据breedType赛选一下养殖场,从breedFactoryIds中去掉不是这个养殖类型的养殖场ID

            if (breedType != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("breedType",breedType);
                if(!breedFactoryIds.isEmpty()) {
                    map.put("breedFactoryIds",breedFactoryIds);
                    breedFactoryIds = breedFactoryDao.selectedByType(map);
                }
            }
            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(fromDate!=null) criteria.put("fromDate", fromDate);

            if(toDate != null) criteria.put("toDate",toDate);

            criteria.put("status", PollutionControlStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(pollutionControlDao.pagingPollutionControls(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find pollutionControl fromDate={},fromDate={},toDate={}),cause:{}", fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("pollutionControl.find.fail");
        }
    }


    /**
     * 根据养殖场id查询
     * @param breedFactoryId
     * @return
     */
    @Override
    public Response<PollutionControl> findByFactoryId(Long breedFactoryId){

            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("breedFactoryId",breedFactoryId);
            Paging<PollutionControl> paging = pollutionControlDao.paging(1,1,criteria);
            if(paging.getData().size()>0){
                return Response.ok(paging.getData().get(0));
            }
            return Response.fail("failed to find factoryId");

    }

    @Override
    public Response<Paging<PollutionControl>> pagingAll(Integer pageNo, Integer pageSize,List<Long> ids,Date fromDate, Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryIds",ids);

            if(fromDate!=null) criteria.put("fromDate", fromDate);

            if(toDate != null) criteria.put("toDate",toDate);

            criteria.put("status", PollutionControlStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(pollutionControlDao.pagingPollutionControls(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find pollutionControl fromDate={},fromDate={},toDate={}),cause:{}", fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("pollutionControl.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> sumPollutionForWeb(Map<String, Object> map) {
        try{
            return Response.ok(pollutionControlDao.sumForWeb(map));
        }catch (Exception e){
            log.error("failed to sum pollution for web , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to sum pollution for web");
        }
    }

}
